package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.GlassFluorescenceReq;
import com.ruibang.glass.produce.domain.req.MainIngredientsRecordReq;
import com.ruibang.glass.produce.domain.resp.MainIngredientsRecordTrendChart;
import com.ruibang.glass.produce.entity.MainIngredientsRecord;
import com.ruibang.glass.produce.mapper.MainIngredientsRecordMapper;
import com.ruibang.glass.produce.service.MainIngredientsRecordService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import com.teaming.cloud.framework2.autoconfigure.dynamic.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-03-11  14:27
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
@DS("emc")
public class MainIngredientsRecordServiceImpl extends ServiceImpl<MainIngredientsRecordMapper, MainIngredientsRecord> implements MainIngredientsRecordService {


    @Override
    public IPage<MainIngredientsRecord> pageQuery(MainIngredientsRecordReq mainIngredientsRecordReq) {
        QueryWrapper<MainIngredientsRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(MainIngredientsRecord::getDelFlag, Constants.ZERO_STR);
        if (mainIngredientsRecordReq.getStartTime() != null && mainIngredientsRecordReq.getEndTime() != null) {
            qw.lambda().between(MainIngredientsRecord::getEndTime, mainIngredientsRecordReq.getStartTime(), mainIngredientsRecordReq.getEndTime());
        }
        qw.lambda().like(StringUtils.isNotBlank(mainIngredientsRecordReq.getMaterialCode()), MainIngredientsRecord::getMaterialCode, mainIngredientsRecordReq.getMaterialCode());
        qw.lambda().like(StringUtils.isNotBlank(mainIngredientsRecordReq.getMixer()), MainIngredientsRecord::getMixer, mainIngredientsRecordReq.getMixer());
        qw.lambda().like(StringUtils.isNotBlank(mainIngredientsRecordReq.getTankNumber()), MainIngredientsRecord::getTankNumber, mainIngredientsRecordReq.getTankNumber());
        qw.lambda().like(StringUtils.isNotBlank(mainIngredientsRecordReq.getJobManager()), MainIngredientsRecord::getJobManager, mainIngredientsRecordReq.getJobManager());
        qw.lambda().orderByDesc(MainIngredientsRecord::getStartTime);
        return this.page(new Page<>(mainIngredientsRecordReq.getPageNo(), mainIngredientsRecordReq.getPageSize()), qw.lambda());
    }

    @Override
    public void deleteById(String id) {
        lambdaUpdate().eq(MainIngredientsRecord::getId, id).set(MainIngredientsRecord::getDelFlag, Constants.ONE_STR).update();
    }

    @Override
    public MainIngredientsRecordTrendChart trendChart(GlassFluorescenceReq glassFluorescenceReq) {
        if (glassFluorescenceReq.getStartDate() == null || glassFluorescenceReq.getEndDate() == null) {
            //默认查询7天
            LocalDate now = LocalDate.now();
            glassFluorescenceReq.setStartDate(now.minusDays(7));
            glassFluorescenceReq.setEndDate(now);
        }
        //组合数据
        MainIngredientsRecordTrendChart mainIngredientsRecordTrendChart = new MainIngredientsRecordTrendChart();
        List<MainIngredientsRecord> data = baseMapper.trendChart(glassFluorescenceReq);
        if (CollectionUtils.isNotEmpty(data)) {
            mainIngredientsRecordTrendChart.setErrorTotalWeight(data.stream().map(MainIngredientsRecord::getErrorTotalWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setAErrorWeight(data.stream().map(MainIngredientsRecord::getAErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setBErrorWeight(data.stream().map(MainIngredientsRecord::getBErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setCErrorWeight(data.stream().map(MainIngredientsRecord::getCErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setDErrorWeight(data.stream().map(MainIngredientsRecord::getDErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setEErrorWeight(data.stream().map(MainIngredientsRecord::getEErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setFErrorWeight(data.stream().map(MainIngredientsRecord::getFErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setGErrorWeight(data.stream().map(MainIngredientsRecord::getGErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setHErrorWeight(data.stream().map(MainIngredientsRecord::getHErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setIErrorWeight(data.stream().map(MainIngredientsRecord::getIErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setJErrorWeight(data.stream().map(MainIngredientsRecord::getJErrorWeight).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setJobNumber(data.stream().map(MainIngredientsRecord::getJobNumber).collect(Collectors.toList()));
            mainIngredientsRecordTrendChart.setCollectTime(data.stream().map(e -> e.getDate().toString()).collect(Collectors.toList()));
        }
        return mainIngredientsRecordTrendChart;
    }

    @Override
    public void export(MainIngredientsRecordReq mainIngredientsRecordReq, HttpServletResponse response) {
        mainIngredientsRecordReq.setPageNo(-1);
        mainIngredientsRecordReq.setPageSize(-1);
        IPage<MainIngredientsRecord> page = this.pageQuery(mainIngredientsRecordReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<MainIngredientsRecord> mainIngredientsRecordList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), MainIngredientsRecord.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(mainIngredientsRecordList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public List<MainIngredientsRecord> getIngredientInfoByDate(LocalDate date) {
        return baseMapper.getIngredientInfoByDate(date);
    }
}
